#!/bin/bash

# Codebase RAG MCP CLI wrapper
# Usage: codebase-rag <command>

# Find the installation directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Check if this script is in PATH, if so, find the actual installation
if [[ "$SCRIPT_DIR" == *"/bin" ]]; then
    # Try to find the installation in common locations
    POSSIBLE_DIRS=(
        "/home/linden/Downloads/minimax/roo-code-index-mcp/codebase-rag-mcp"
        "$HOME/codebase-rag-mcp"
        "$(pwd)/codebase-rag-mcp"
    )
    
    for dir in "${POSSIBLE_DIRS[@]}"; do
        if [ -f "$dir/cli.js" ]; then
            SCRIPT_DIR="$dir"
            break
        fi
    done
fi

# Check if node is available
if ! command -v node &> /dev/null; then
    echo "❌ Error: Node.js is required but not installed."
    exit 1
fi

# Check if the CLI script exists
if [ ! -f "$SCRIPT_DIR/cli.js" ]; then
    echo "❌ Error: cli.js not found in $SCRIPT_DIR"
    echo "Please run this command from the codebase-rag-mcp directory, or:"
    echo "1. cd /home/linden/Downloads/minimax/roo-code-index-mcp/codebase-rag-mcp"
    echo "2. ./codebase-rag <command>"
    exit 1
fi

# Capture the current working directory before changing to script directory
ORIGINAL_PWD="$(pwd)"

# Execute the Node.js CLI with all arguments
# Pass the original working directory as an environment variable
cd "$SCRIPT_DIR"
CODEBASE_RAG_PWD="$ORIGINAL_PWD" node cli.js "$@"